package cn.javass.dp.flyweight.example1;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂
 */
public class FlyweightFactory {

    /**
     * 缓存多个flyweight工厂
     */
    private Map<String, Flyweight> fsMap = new HashMap<>();


    /**
     * 获取key对应的享元对象
     * @param key
     * @return
     */
    public Flyweight getFlyweight(String key) {
        //1先从缓存中查找，是否存在key对应的Flyweight对象
        Flyweight f = fsMap.get(key);
        if (f == null) {
            f = new ConcreteFlyweight(key);
            fsMap.put(key, f);
        }
        return f;
    }
}
